<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/logo.png"/>
            </div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm"  class="login_form">
                <el-form-item  prop="username" >
                    <el-input v-model="ruleForm.username" prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <el-form-item  prop="password">
                    <el-input v-model="ruleForm.password" prefix-icon="el-icon-lock"  ></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                ruleForm: {
                    username: 'admin',
                    password: '123456'

                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate(async valid => {
                    if (valid) {
                      const {data:res}= await  this.$http.post("login",this.ruleForm);
                      if(res.meta.status!==200){
                          this.$message.error('登录失败')
                      }else{
                          this.$message.success("登录成功")
                          window.sessionStorage.setItem("token",res.data.token)
                          this.$router.push("/home")
                      }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }

    }
</script>

<style lang="less" scoped>
    .login_container {
        height: 100%;
        background-color: green;
    }
    .login_box{
        width: 450px;
        height:380px;
        background-color: white;
        border-radius:3px;
        position: absolute;
        left: 50%;
        top:50%;
        transform:translate(-50%,-50%);
        .avatar_box{
            width: 130px;
            height: 130px;
            border:1px solid #eeeeee;
            border-radius: 50%;
            padding:10px;
            box-shadow:  10px 10px 5px #888888;
            margin:-65px auto;
            background-color: #ffffff;
        }
        img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #42b983;
        }
        .login_form{
            position: absolute;
            bottom:0px;
            width:100%;
            padding:0px 20px;
            box-sizing:border-box;
        }
    }
</style>